package com.jxwifi.cloud.quickclean.address;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.idroid.utils.NoDoubleClickListener;
import com.jxwifi.cloud.quickclean.R;
import com.jxwifi.cloud.quickclean.adapter.ClientAddressListAdapter;
import com.jxwifi.cloud.quickclean.app.AccountMgr;
import com.jxwifi.cloud.quickclean.app.StateUtils;
import com.jxwifi.cloud.quickclean.app.UrlPool;
import com.jxwifi.cloud.quickclean.bean.CustomerAddressBean;
import com.jxwifi.cloud.quickclean.model.AuzImpl;
import com.jxwifi.cloud.quickclean.okhttp.Params;
import com.jxwifi.cloud.quickclean.utils.KAlertDialog;
import com.jxwifi.cloud.quickclean.utils.ToastUtils;

import java.util.List;

public class AddressListPresenter {

    private Context mContext;
    private AddressListView mView;
    private AuzImpl mAuz;

    private List<CustomerAddressBean> mListBean;
    private ClientAddressListAdapter mAdapter;

    public AddressListPresenter(Context context, AddressListView View) {
        this.mContext = context;
        this.mView = View;
        mAuz = new AuzImpl();
    }

    public void getdata() {
        mAuz.auzget(UrlPool.CUSTOMER_ADDRESS_LIST, new Params(), new AuzImpl.AuzListener() {
            @Override
            public void AuzSuccess(int code, String data) {
                if (code == 0) {

                    mListBean = JSON.parseArray(data, CustomerAddressBean.class);
                    mView.getListSize(mListBean.size());
                    if (mListBean.size()!=0||mListBean!=null){
                        onDataList();
                    }else{
                        mAdapter.setEmptyView(mView.getViews());
                    }
                } else {
                    mView.ondata();
                }
            }

            @Override
            public void AuzFailure(int errorCode, String msg) {
                mView.onFailure(errorCode, msg);
            }
        });
    }

    public void onDataList(){
        mView.getRView().setLayoutManager(new LinearLayoutManager(mContext));
        mView.getRView().setAdapter(mAdapter = new ClientAddressListAdapter(R.layout.list_address_item, mListBean));
        mAdapter.openLoadAnimation();
        mAdapter.openLoadAnimation(BaseQuickAdapter.FOOTER_VIEW);
        mAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()){
                    case R.id.rel_address_item:
                        if (StateUtils.YES.equals(mListBean.get(position).getIsDefault())){
                            mView.onDefaultaddress();
                        }else{
                            onDefault(mListBean.get(position).getId());
                        }
                        break;
                    case R.id.tv_address_item_edit:
                        //Toast.makeText(mContext, "selectagreed" + position, Toast.LENGTH_SHORT).show();
                        Intent intent=new Intent();
                        intent.setClass(mContext,AddressActivity.class);
                        intent.putExtra("addorcompile","compile");
                        intent.putExtra("customeraddress",mListBean.get(position));
                        mContext.startActivity(intent);
                        break;
                    case R.id.tv_address_item_delete:
                        dialogshow(mListBean.get(position).getId());
                        break;
                    case R.id.tv_address_item_selectagreed:

                        break;
                }
            }
        });
    }

    public void dialogshow(final String DefaultID){
        final KAlertDialog dialogshow=new KAlertDialog(mContext,"确定要删除这个地址吗？");
        dialogshow.setRightTxt("确定", new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                onDelete(DefaultID);
                dialogshow.dismiss();
            }
        });
        dialogshow.setLeftTxt("取消",new NoDoubleClickListener(){
            @Override
            protected void onNoDoubleClick(View v) {
                dialogshow.dismiss();
            }
        });
        dialogshow.show();
    }

    public void onDefault(final String DefaultID){
        Params params=new Params();
        params.add("id",DefaultID);
        mAuz.auzget(UrlPool.UPDATA_IS_DEFAULT, params, new AuzImpl.AuzListener() {
            @Override
            public void AuzSuccess(int code, String data) {
                if (code==0){
                    mView.onDefaultaddress();
                }else{
                    mView.onDefaults(DefaultID);
                }
            }

            @Override
            public void AuzFailure(int errorCode, String msg) {
                mView.onFailure(errorCode, msg);
            }
        });
    }

    public void onDelete(final String DefaultID){
        Params params=new Params();
        params.add("id",DefaultID);
        mAuz.auzget(UrlPool.ADDRESS_DELETE, params, new AuzImpl.AuzListener() {
            @Override
            public void AuzSuccess(int code, String data) {
                if (code==0){
                    mView.onDeleteaddress();
                }else{
                    mView.onDelete(DefaultID);
                }
            }

            @Override
            public void AuzFailure(int errorCode, String msg) {
                mView.onFailure(errorCode, msg);
            }
        });
    }
}
